package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.admin.dtos.AdUserDto;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.admin.vo.AdUserVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import com.heima.admin.mapper.AdUserMapper;
import com.heima.admin.service.AdUserService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    @Override
    public ResponseResult loginIn(AdUserDto dto) {
        //检查参数
        if (dto == null || StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断用户名是否存在
        LambdaQueryWrapper<AdUser> queryWrapper = Wrappers.lambdaQuery();
        AdUser adUser = getOne(queryWrapper.eq(AdUser::getName,dto.getName()));
        if (adUser == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
        }
        //获取盐并生成当前输入的密码
        String psw = DigestUtils.md5DigestAsHex((dto.getPassword()+adUser.getSalt()).getBytes());
        //判断密码是否正确
        if(!psw.equals(adUser.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //判断用户状态
        if (!(adUser.getStatus() ==9)){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        //修改登录时间
        adUser.setLoginTime(new Date());
        updateById(adUser);
        //颁发token
        String token = AppJwtUtil.getToken(adUser.getId().longValue());
        //封装userVO
        AdUserVO userVO = new AdUserVO();
        BeanUtils.copyProperties(adUser,userVO);

        //封装返回result里的data
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("token", token);
        map.put("user", userVO);

        return ResponseResult.okResult(map);

    }

    @Override
    public ResponseResult addAdUser(AdUser user) {
        if (user == null || StringUtils.isBlank(user.getName()) || StringUtils.isBlank(user.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"账号密码不能为空");
        }
        //生成盐
        String salt = RandomStringUtils.randomAlphanumeric(8);
        //密码加盐生成md5
        String password = DigestUtils.md5DigestAsHex((user.getPassword()+salt).getBytes());
        //装载AdUser信息
        user.setCreatedTime(new Date());//注册时间
        user.setSalt(salt);
        user.setPassword(password);
        user.setStatus(9);//初始化状态
        //向数据库添加
        save(user);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
